package com.groupbuying.payproducer.payCenterStrategy.iml;

import com.groupbuying.payproducer.connector.SysconfigFeign;
import com.groupbuying.payproducer.constants.CommonConstants;
import com.groupbuying.payproducer.constants.PayCenterTypeConstants;
import com.groupbuying.payproducer.constants.PayInfoEnumConstants;
import com.groupbuying.payproducer.dao.PayInfoDao;
import com.groupbuying.payproducer.domain.PayInfoDO;
import com.groupbuying.payproducer.domain.PayOrderRelDO;
import com.groupbuying.payproducer.service.PayOrderRelService;
import com.groupbuying.payproducer.payCenterStrategy.PayStrategy;
import com.groupbuying.payproducer.utils.apiresult.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 积分支付实现
 * @Author Vincentxin
 * @Date 2019-05-20
 */
@Component
public class PlatFormCurrencyPayStrategy implements PayStrategy {
    @Autowired
    private PayInfoDao payInfoDao;

    @Autowired
    private SysconfigFeign sysconfigFeign;

    @Autowired
    private PayOrderRelService payOrderRelService;
    @Override
    public ApiResult unifiedPayment(Map<String,String> mapParam, PayCenterTypeConstants type) {
        //生成预支付信息
        PayInfoDO payInfoDO = new PayInfoDO();
        payInfoDO.setId(sysconfigFeign.getPayID());
        payInfoDO.setIsDelete(CommonConstants.COMMON_FLAG_UNDEL);
        payInfoDO.setGmtCreate(new Date());
        payInfoDO.setGmtModified(new Date());
        payInfoDO.setPayState(PayInfoEnumConstants.ENUM_PAYSTATE_UNPAY);
        payInfoDO.setPayType(PayInfoEnumConstants.ENUM_PAYTYPE_CUR);
        payInfoDO.setPayChannel(PayInfoEnumConstants.ENUM_PAYCHANNEL_SELF);
        payInfoDO.setPayMoney(new BigDecimal("0"));
        payInfoDO.setPayBalance(new BigDecimal("0"));
        payInfoDO.setPayCur(new BigDecimal(mapParam.get("payCurrency")));
        payInfoDao.save(payInfoDO);

        //生成订单关联
        PayOrderRelDO payOrderRelDO = new PayOrderRelDO();
        payOrderRelDO.setIsDelete(CommonConstants.COMMON_FLAG_UNDEL);
        payOrderRelDO.setOrderId(mapParam.get("orderId"));
        payOrderRelDO.setId(payInfoDO.getId());
        payOrderRelDO.setGmtCreate(new Date());
        payOrderRelDO.setGmtModified(new Date());
        payOrderRelService.save(payOrderRelDO);

        return ApiResult.success(payInfoDO);
    }
}
